<?php

class Application_Model_ForumReplyMapper extends Ahs_Model_Mapper
{
    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Replies';
    }
    
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('reply_id = ?' => $id));
        return $this->_toReply($row);
    }
    
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();

        return $this->_toReplies($rows);
    }
    public function addReply($topicid, $creator, $message, $creationdate, $modifieddate)

    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $count = $db->insert('topics', array('reply_topic_id' => $topicid, 'reply_creator' => $creator,  'reply_message' => $message, 'reply_creationdate' => $creationdate, 'reply_modifieddate' => $modifieddate));
        if($count > 0)
        {
            return true;
        }
        return false;
    }
    public function deleteReply($replyid)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $affectedrows=$db->delete('replies', array('reply_id = ?' =>$replyid));
        if($affectedrows > 0)
        {
            return true;
        }
        return false;
    }
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Reply 
     */
    private function _toReply($row = null)
    {
        $reply = new Application_Model_ForumReply();

        if ($row) {
            $reply->Id            = $row['reply_id'];
            $reply->TopicId       = $row['reply_topic_id'];
            $reply->Creator       = $row['reply_creator'];
            $reply->Message       = $row['reply_message'];
            $reply->Creationdate  = $row['reply_creationdate'];
            $reply->Modifieddate  = $row['reply_modifieddate'];   
            $reply->Deleted       = $row['reply_deleted'];
        }
        return $reply;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toReplies($rows = null)
    {
        $replies = array();

        if ($rows) {
            foreach ($rows as $row) { 
                $replies[] = $this->_toReply($row);
            }
        }
        return $replies;
    } 
}